#coding=utf-8
'''
Created on 2013-9-3

@author: Ben

武将模块
'''

from ..common import Command, Logger, RequestModel
from ..common.SocketService import gameSocketService
import GeneralData
import threading

'''
获取武将信息
'''
class GeneralGetInfoRequest(RequestModel.RequestModel):
    
    def setCommand(self):
        self.command = Command.GET_GENERAL_INFO
        
    def onReceive(self, data):
        Logger.log('获取武将信息成功', output = True)
        GeneralData.init(data)
    
'''
武将信息更新
'''
def generalInfoUpdate(data):
    GeneralData.update(data)

'''
使用募兵令
'''
def tokenRecruit(generalId):
    command = Command.TOKEN_RECRUIT
    params = 'generalId=' + str(generalId)
    gameSocketService.send(command, params, tokenRecruitCallback)
    
'''
使用募兵令回调函数
'''
def tokenRecruitCallback(data):
    if 'consumeToken' in data:
        Logger.log('使用了' + str(data["consumeToken"]) + '个募兵令')
    if "token" in data:
        GeneralData.token = data["token"]
        
'''
牢房逃跑
'''
def escape(generalId):
    command = Command.ESCAPE
    params = 'generalId=' + str(generalId)
    gameSocketService.send(command, params, escapeCallback)
    
'''
牢房逃跑回调函数
'''
def escapeCallback(data):
    Logger.log('开始从牢房逃跑', output = True)
    #刷新武将数据
    GeneralGetInfoRequest(None, None, 0, 0).send()
    #逃跑倒计时结束后再次刷新武将数据
    if "cd" in data:
        cd = int(data["cd"] / 1000) + 1
        threading.Timer(cd, GeneralGetInfoRequest(None, None, 0, 0).send).start()